package com.lc.pianostudent.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.lc.pianostudent.BaseApplication;
import com.lc.pianostudent.activity.NavigationActivity;
import com.lc.pianostudent.activity.VipActivity;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.zcx.helper.app.AppApplication;
import com.zcx.helper.http.Http;
import com.zcx.helper.util.UtilToast;

/**
 * 微信登录
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        BaseApplication.WeiXinPay.handleIntent(getIntent(), this);

    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        setIntent(intent);

        BaseApplication.WeiXinPay.handleIntent(getIntent(), this);

    }

    @Override
    public void onResp(BaseResp resp) {

        Log.e("微信支付回调", resp + "");

        Log.e("分享回调:","PAY" + resp.errStr);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {

                case 0:

                    if(BaseApplication.BasePreferences.readChoose().equals("1")){
                        UtilToast.show("支付成功，请重新登录");
                        BaseApplication.BasePreferences.setIsLogin(false);
                        BaseApplication.INSTANCE.finishActivity(VipActivity.class);

                        if (NavigationActivity.onClickProduct != null) {
                            NavigationActivity.onClickProduct.setOnClick();
                        }
                    }else {
                        BaseApplication.INSTANCE.finishActivity(VipActivity.class);
                    }


                    break;

                case -1:

                    UtilToast.show("签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等");

                    break;

                case -2:

                    UtilToast.show("取消支付");

                    break;

            }

        }

        Http.getInstance().dismiss();

        finish();

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

}